package com.weishi.project.api.controller.thread;

import com.alibaba.fastjson.JSONObject;
import com.weishi.common.enums.RedisConstants;
import com.weishi.framework.redis.RedisCache;
import com.weishi.project.api.controller.selection.WeekPlanSelection;
import com.weishi.project.training.domain.HeartRateCount;
import com.weishi.project.training.service.IHeartRateCountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 增删改
 */
@Component
public class WeekPlanRunnable implements Runnable {


    /**
     * redis 数据库
     */
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RedisCache redisCache;

    /**
     * 心率计次数据
     */
    @Autowired
    private IHeartRateCountService heartRateCountService;

    @Override
    public void run() {
        /**
         * 线程池高并发，使用同步线程做处理
         */
        //同步代码块锁
        synchronized (WeekPlanRunnable.class) {
            /**
             * 教练员端 -- 点击计次按钮
             */
            if (WeekPlanSelection.getValue().equals("ApiWeekPlanServiceImpl_countTimes")) {
                try {
                    JSONObject jsonObject = (JSONObject) redisTemplate.boundListOps("ApiWeekPlanServiceImpl_countTimes").rightPop();
                    int channelId = Integer.parseInt(jsonObject.get("channelId").toString());
                    int groupId = Integer.parseInt(jsonObject.get("groupId").toString());

                    // 查询分段序号的数字
                    List<HeartRateCount> heartRateCounts = heartRateCountService.listZHKID(channelId);
                    HeartRateCount heartRateCount = new HeartRateCount();
                    if(heartRateCounts!=null && heartRateCounts.size()>0){
                        heartRateCount.setStage(heartRateCounts.get(heartRateCounts.size()-1).getStage() + 1);
                    }else{
                        heartRateCount.setStage(1);
                    }

                    //新增计次
                    heartRateCount.setChannelId(channelId);
                    heartRateCount.setGroupId(groupId);
                    heartRateCount.setCtime(new Date());
                    heartRateCount.setPointAchievement(jsonObject.get("pointAchievement").toString());
                    heartRateCountService.saveAndAddRecord(heartRateCount);

                    //当前航道计次Id,方便统计
                    Integer index = redisCache.getCacheObject(RedisConstants.Channel_count_Prefix + channelId);
                    if(index==null){
                        index = 1;
                    }
                    redisCache.setCacheObject(RedisConstants.Channel_count_Prefix + channelId, ++index, 3, TimeUnit.HOURS);

                } catch (Exception e) {
                    e.printStackTrace();
                }
                /**
                 * 教练员端 -- 点击计次按钮 结束
                 */
            }
        }
    }
}
